package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
)

// 获取提现记录信息
func GetWithdrawRecordInfo(p map[string]interface{}) *models.WithdrawRecord {
	o := orm.NewOrm()
	withdrawRecord := &models.WithdrawRecord{}
	qs := o.QueryTable("WithdrawRecord")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["sn"]; ok {
		qs = qs.Filter("sn", p["sn"])
	}
	qs.One(withdrawRecord)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if withdrawRecord.Id > 0 {
			withdrawRecord.StatusShow = models.WithdrawRecordStatus[withdrawRecord.Status]
			withdrawRecord.CreatedShow = withdrawRecord.Created.Format("2006-01-02 15:04:05")
			withdrawRecord.CreatedShowDate = withdrawRecord.Created.Format("2006.01.02")
		}
	}
	return withdrawRecord
}

// 添加提现记录信息
func AddWithdrawRecord(input url.Values) (int64, error) {
	o := orm.NewOrm()
	withdrawRecord := &models.WithdrawRecord{}
	sn := utils.GetSn()
	p := map[string]interface{}{}
	p["sn"] = sn
	withdrawRecordInfo := GetWithdrawRecordInfo(p)
	for withdrawRecordInfo.Id > 0 {
		sn = utils.GetSn()
		p = map[string]interface{}{}
		p["sn"] = sn
		withdrawRecordInfo = GetWithdrawRecordInfo(p)
	}
	withdrawRecord.Sn = sn
	user := &models.User{}
	if _, ok := input["user_id"]; ok {
		user.Id = utils.Atoi64(input["user_id"][0])
		o.Read(user)
	}
	withdrawRecord.User = user
	if _, ok := input["amount"]; ok {
		withdrawRecord.Amount = utils.Atof64(input["amount"][0])
	}
	return o.Insert(withdrawRecord)
}

// 修改提现记录信息
func EditWithdrawRecord(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("WithdrawRecord").Filter("id", input["id"][0]).Update(p)
}

// 获取提现记录列表
func GetWithdrawRecordList(p map[string]interface{}) (int64, []*models.WithdrawRecord) {
	var withdrawRecordList []*models.WithdrawRecord
	o := orm.NewOrm()
	qs := o.QueryTable("WithdrawRecord")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("user_id", p["userId"])
	}
	if _, ok := p["realName"]; ok {
		qs = qs.Filter("User__RealName", p["realName"])
	}
	if _, ok := p["phone"]; ok {
		qs = qs.Filter("User__Phone", p["phone"])
	}
	if _, ok := p["status"]; ok {
		qs = qs.Filter("status", p["status"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&withdrawRecordList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, withdrawRecord := range withdrawRecordList {
			withdrawRecord.StatusShow = models.WithdrawRecordStatus[withdrawRecord.Status]
			withdrawRecord.CreatedShow = withdrawRecord.Created.Format("2006-01-02 15:04:05")
			withdrawRecord.CreatedShowDate = withdrawRecord.Created.Format("2006.1.2")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, withdrawRecord := range withdrawRecordList {
			withdrawRecord.StatusShow = models.WithdrawRecordStatus[withdrawRecord.Status]
			withdrawRecord.CreatedShowDate = withdrawRecord.Created.Format("2006.1.2")
		}
	}
	return totalCount, withdrawRecordList
}